package com.micro.train.controller;

import com.micro.train.entity.Station;
import com.micro.train.service.StationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

/**
 * 车站信息控制器
 */
@Tag(name = "车站信息管理", description = "车站信息相关接口")
@RestController
@RequestMapping("/api/stations")
public class StationController {
    
    @Autowired
    private StationService stationService;
    
    @Operation(summary = "添加车站信息")
    @PostMapping
    public ResponseEntity<Station> addStation(@RequestBody Station station) {
        Station savedStation = stationService.addStation(station);
        return ResponseEntity.ok(savedStation);
    }
    
    @Operation(summary = "更新车站信息")
    @PutMapping("/{stationId}")
    public ResponseEntity<Station> updateStation(@PathVariable Long stationId, @RequestBody Station station) {
        Station updatedStation = stationService.updateStation(stationId, station);
        return ResponseEntity.ok(updatedStation);
    }
    
    @Operation(summary = "删除车站信息")
    @DeleteMapping("/{stationId}")
    public ResponseEntity<Void> deleteStation(@PathVariable Long stationId) {
        stationService.deleteStation(stationId);
        return ResponseEntity.noContent().build();
    }
    
    @Operation(summary = "获取所有车站信息")
    @GetMapping
    public ResponseEntity<List<Station>> getAllStations() {
        List<Station> stations = stationService.getAllStations();
        return ResponseEntity.ok(stations);
    }
    
    @Operation(summary = "根据ID获取车站信息")
    @GetMapping("/{stationId}")
    public ResponseEntity<Map<String, Object>> getStationById(@PathVariable Long stationId) {
        Station station = stationService.getStationById(stationId);
        if (station == null) {
            return ResponseEntity.notFound().build();
        }
        
        Map<String, Object> stationMap = new HashMap<>();
        stationMap.put("站点ID", station.getStationId());
        stationMap.put("站点名称", station.getStationName());
        stationMap.put("站点代码", station.getStationCode());
        stationMap.put("城市代码", station.getCityCode());
        stationMap.put("城市名称", station.getCityName());
        stationMap.put("省份", station.getProvince());
        stationMap.put("状态", station.getStatus());
        
        return ResponseEntity.ok(stationMap);
    }
    
    @Operation(summary = "根据车站代码查询车站")
    @GetMapping("/code/{stationCode}")
    public ResponseEntity<Station> getStationByCode(@PathVariable String stationCode) {
        Station station = stationService.getStationByCode(stationCode);
        return ResponseEntity.ok(station);
    }
    
    @Operation(summary = "根据城市代码查询车站")
    @GetMapping("/city-code/{cityCode}")
    public ResponseEntity<List<Station>> getStationsByCityCode(@PathVariable String cityCode) {
        List<Station> stations = stationService.getStationsByCityCode(cityCode);
        return ResponseEntity.ok(stations);
    }
    
    @Operation(summary = "根据城市名称查询车站")
    @GetMapping("/city-name/{cityName}")
    public ResponseEntity<List<Station>> getStationsByCityName(@PathVariable String cityName) {
        List<Station> stations = stationService.getStationsByCityName(cityName);
        return ResponseEntity.ok(stations);
    }
    
    @Operation(summary = "根据省份查询车站")
    @GetMapping("/province/{province}")
    public ResponseEntity<List<Station>> getStationsByProvince(@PathVariable String province) {
        List<Station> stations = stationService.getStationsByProvince(province);
        return ResponseEntity.ok(stations);
    }
    
    @Operation(summary = "根据状态查询车站")
    @GetMapping("/status/{status}")
    public ResponseEntity<List<Station>> getStationsByStatus(@PathVariable Integer status) {
        List<Station> stations = stationService.getStationsByStatus(status);
        return ResponseEntity.ok(stations);
    }
    
    @Operation(summary = "根据车站名称模糊查询")
    @GetMapping("/search")
    public ResponseEntity<List<Station>> getStationsByNameLike(@RequestParam String stationName) {
        List<Station> stations = stationService.getStationsByNameLike(stationName);
        return ResponseEntity.ok(stations);
    }
}
